using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Advanced_Terrain
{
    public class Basket : DrawableGameComponent
    {
       ContentManager _content;
       Model _model;
       Matrix[] _boneTransforms;
       public Vector3 Position;
       float Scale = .0043f;
       public Quaternion Rotation = Quaternion.Identity;
        private Texture2D _Texture;
        public GraphicsDevice graphics;
        public Vector3 DiscInitialPosition;

        public Vector3 DropZone;
        public Boolean IsVisible = true;
        
        public Basket(Game game, ContentManager content) : base(game)
        {
            _content = content;
        }
        /*public void setPosition(Vector3 Position)
        {
            this.Position =  Position;
        }*/
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            _model = _content.Load<Model>("basket");
            // Allocate the transform matrix array.
            _boneTransforms = new Matrix[_model.Bones.Count];
            //_Texture = _content.Load<Texture2D>("Innova");
            //rotate the model 90 degrees
            //_model.Root.Transform *= Matrix.CreateRotationX(MathHelper.ToRadians(90));

            base.LoadGraphicsContent(loadAllContent);
        }

        public override void Draw(GameTime gameTime)
        {
            
            if(IsVisible)
            {
            _model.CopyAbsoluteBoneTransformsTo(_boneTransforms);
            // Draw the model.
            foreach (ModelMesh mesh in _model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Begin();
                    effect.Projection = CameraProperties.projectionMatrix;
                    effect.World = _boneTransforms[mesh.ParentBone.Index] * Matrix.CreateScale(Scale);
                    effect.World = effect.World * Matrix.CreateFromQuaternion(Rotation) * Matrix.CreateTranslation(Position);
                    effect.View = CameraProperties.View;
                    effect.EnableDefaultLighting();
                    effect.End();
                }

                mesh.Draw();

            }
            }

            base.Draw(gameTime);
        }
       
       
     
       public void ResetDisc()
       {
           Rotation = Quaternion.Identity;
         
       }
        public void UpdatePosition()
        {
            this.Position.Y = Game1.newTerrain.GetTerrainHeight(this.Position.X, this.Position.Z * -1);
        }
        public void LoadImage(Texture2D HoleMap)
        {
            float holeData;
            int terrainWidth;
            int terrainLength;
            //Texture2D HoleMap =  + "//holemap");
            terrainWidth = HoleMap.Width;
            terrainLength = HoleMap.Height;
            Color[] HoleMapColors = new Color[terrainWidth * terrainLength];
            Color TestingColor = Color.White;
            HoleMap.GetData(HoleMapColors);
            holeData = 0;
            for (int y = 0; y < terrainWidth; y++)
                for (int x = 0; x < terrainLength; x++)
                {
                    TestingColor = HoleMapColors[x + ((terrainWidth - 1) - y) * terrainWidth];
                    if (TestingColor.R == TestingColor.B)
                        holeData = HoleMapColors[x + ((terrainWidth - 1) - y) * terrainWidth].R;
                    else
                        holeData = 128;
                    if (holeData == 255)
                    {
                        Position.X = x;
                        Position.Z = y * -1;
                    }
                    if (holeData == 0)
                    {
                        DiscInitialPosition.X = x;
                        DiscInitialPosition.Z = y * -1;
                    }
                    if (TestingColor.B == 255)
                    {
                        if (TestingColor.R != 255)
                        {
                            DropZone.X = x;
                            DropZone.Z = y * -1;
                        }
                    }
                }
            if (DropZone.X == 0 && DropZone.Z == 0)
            {
                DropZone.X = DiscInitialPosition.X;
                DropZone.Z = DiscInitialPosition.Z;
            }
        }
        


    }
    
}
